home *** CD-ROM | disk | FTP | other *** search
/ Amiga CD-Sensation: Golden Games / Amiga CD-Sensation - Ausgabe 2 - Golden Games (1996)(GTI - Schatztruhe)(DE)[!].iso / Brain Activity / Tetris / TetrisImages.c < prev    next >
C/C++ Source or Header  |  1992-11-18  |  7KB  |  237 lines

  1. /*
  2. Copyright (c) 1992, Trevor Smigiel.  All rights reserved.
  3.  
  4. (I hope Commodore doesn't mind that I borrowed their copyright notice.)
  5.  
  6. The source and executable code of this program may only be distributed in free
  7. electronic form, via bulletin board or as part of a fully non-commercial and
  8. freely redistributable diskette.  Both the source and executable code (including
  9. comments) must be included, without modification, in any copy.  This example may
  10. not be published in printed form or distributed with any commercial product.
  11.  
  12. This program is provided "as-is" and is subject to change; no warranties are
  13. made.  All use is at your own risk.  No liability or responsibility is assumed.
  14.  
  15. */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <graphics/gfx.h>
  20. #include <graphics/rastport.h>
  21. /* This comment is for the includesym.ced macro which searches for first comment*/
  22. #include <proto/exec.h>
  23. #include <proto/graphics.h>
  24. #include <proto/intuition.h>
  25.  
  26. #include <stdio.h>
  27.  
  28. #include "Tetris.h"
  29.  
  30. static Point TPoints[148] = {
  31. /*   0 */    {0, -2}, {0,  0}, {-2,  0}, {-2, -2}, {0, -2}, 
  32.  
  33. /*   5 */    {0, -1}, {0,  0}, {-4,  0}, {-4, -1}, {0, -1}, 
  34. /*  10 */    {0, -4}, {0,  0}, {-1,  0}, {-1, -4}, {0, -4}, 
  35.  
  36. /*  15 */    {0, -1}, {0,  0}, {-3,  0}, {-3, -2}, { 2, -2}, { 2, -1}, {0, -1}, 
  37. /*  22 */    {0, -3}, {0,  2}, { 1,  2}, { 1,  0}, {-2,  0}, {-2, -3}, {0, -3}, 
  38. /*  29 */    {0, -2}, {0,  0}, {-1,  0}, {-1,  1}, {-3,  1}, {-3, -2}, {0, -2}, 
  39. /*  36 */    {0, -3}, {0,  0}, {-2,  0}, {-2, -1}, {-1, -1}, {-1, -3}, {0, -3}, 
  40.  
  41. /*  43 */    {0, -2}, {0,  0}, {-3,  0}, {-3, -1}, {-1, -1}, {-1, -2}, {0, -2}, 
  42. /*  50 */    {0, -1}, {0,  0}, {-2,  0}, {-2, -3}, { 1, -3}, { 1, -1}, {0, -1}, 
  43. /*  57 */    {0, -2}, {0,  1}, { 2,  1}, { 2,  0}, {-3,  0}, {-3, -2}, {0, -2}, 
  44. /*  64 */    {0, -3}, {0,  0}, {-1,  0}, {-1,  2}, {-2,  2}, {-2, -3}, {0, -3}, 
  45.  
  46. /*  71 */    {0, -1}, {0,  0}, {-3,  0}, {-3, -1}, {-2, -1}, {-2, -2}, { 1, -2}, { 1, -1}, {0, -1}, 
  47. /*  80 */    {0, -2}, {0,  1}, { 1,  1}, { 1,  0}, {-2,  0}, {-2, -3}, { 1, -3}, { 1, -2}, {0, -2}, 
  48. /*  89 */    {0, -2}, {0,  1}, { 1,  1}, { 1,  0}, {-2,  0}, {-2,  1}, {-3,  1}, {-3, -2}, {0, -2}, 
  49. /*  98 */    {0, -3}, {0,  0}, {-1,  0}, {-1,  1}, {-2,  1}, {-2, -2}, {-1, -2}, {-1, -3}, {0, -3}, 
  50.  
  51. /* 107 */    {0, -1}, {0,  0}, {-2,  0}, {-2,  1}, {-3,  1}, {-3, -2}, { 1, -2}, { 1, -1}, {0, -1}, 
  52. /* 116 */    {0, -3}, {0,  1}, { 1,  1}, { 1,  0}, {-2,  0}, {-2, -2}, {-1, -2}, {-1, -3}, {0, -3}, 
  53.  
  54. /* 125 */    {0, -2}, {0,  1}, { 1,  1}, { 1,  0}, {-3,  0}, {-3, -1}, {-2, -1}, {-2, -2}, {0, -2}, 
  55. /* 134 */    {0, -2}, {0,  0}, {-1,  0}, {-1,  1}, {-2,  1}, {-2, -3}, { 1, -3}, { 1, -2}, {0, -2}, 
  56.  
  57. /* 143 */    {0, -1}, {0,  0}, {-1,  0}, {-1, -1}, {0, -1}, 
  58. };
  59. static Point TBorders[NBITMAPS] = {
  60.     {4,   0},
  61.     {4,   5}, {4,  10},
  62.     {6,  15}, {6,  22}, {6,  29}, {6,  36},
  63.     {6,  43}, {6,  50}, {6,  57}, {6,  64},
  64.     {8,  71}, {8,  80}, {8,  89}, {8,  98},
  65.     {8, 107}, {8, 116},
  66.     {8, 125}, {8, 134},
  67.     {4, 143},
  68. };
  69. static UWORD TRef[NBITMAPS] = {0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 7};
  70. Point TSizes[NBITMAPS];
  71. extern Point TSSizes[NBITMAPS] = {
  72.     /* width, height */
  73.     { 2,  2},
  74.     { 4,  1}, { 1,  4},
  75.     { 3,  2}, { 2,  3}, { 3,  2}, { 2,  3},
  76.     { 3,  2}, { 2,  3}, { 3,  2}, { 2,  3},
  77.     { 3,  2}, { 2,  3}, { 3,  2}, { 2,  3},
  78.     { 3,  2}, { 2,  3},
  79.     { 3,  2}, { 2,  3},
  80.     { 1,  1},
  81. };
  82.  
  83. struct BitMap TBitMaps[NBITMAPS];
  84. UWORD TColors[8] = {4, 9, 6, 5, 10, 11, 14, 3};
  85. UWORD TFirst[8] = {0, 1, 3, 7, 11, 15, 17, 19};
  86. UWORD TNext[NBITMAPS] = {
  87.     0, 
  88.     2, 1,
  89.     6, 3, 4, 5,
  90.     10, 7, 8, 9,
  91.     14, 11, 12, 13,
  92.     16, 15,
  93.     18, 17,
  94.     19
  95. };
  96. UWORD TPieces[NBITMAPS] = {
  97.     0xcc00,
  98.     0xf000, 0x8888,
  99.     0xe200, 0x44c0, 0x8e00, 0xc880,
  100.     0xe800, 0xc440, 0x2e00, 0x88c0,
  101.     0xe400, 0x4c40, 0x4e00, 0x8c80,
  102.     0xc600, 0x4c80,
  103.     0x6c00, 0x8c40,
  104.     0x8000
  105. };
  106.  
  107. static struct RastPort TetrisRP;
  108. static struct TmpRas TetrisTR;
  109. static struct AreaInfo TetrisAI;
  110. static UBYTE TetrisAIBuf[5 * 12];
  111.  
  112. void
  113. DrawTetrisImage(struct BitMap *bm, WORD which, WORD depth, WORD xsize, WORD ysize)
  114. {
  115.     Point points[10];
  116.     Point *point;
  117.     Point *ppt;
  118.     int pen;
  119.     int i;
  120.     int n;
  121.  
  122.     TetrisRP.BitMap = bm;
  123.  
  124.     n = TBorders[which].x;
  125.     point = &TPoints[TBorders[which].y];
  126.     ppt = &points[0];
  127.     for (i = n; i >= 0; i--, ppt++, point++) {
  128.         ppt->x = point->x * xsize;
  129.         if (ppt->x < 0) ppt->x = (-ppt->x) - 1;
  130.         ppt->y = point->y * ysize;
  131.         if (ppt->y < 0) ppt->y = (-ppt->y) - 1;
  132.     }
  133.  
  134.     switch (depth) {
  135.         case 1: pen = 1; break;
  136.         case 2: pen = 3; break;
  137.         case 3: pen = (TColors[TRef[which]] - 3) % 5 + 3; break;
  138.         default: pen = TColors[TRef[which]]; break;
  139.     }
  140.     SetAPen(&TetrisRP, pen);
  141.     SetBPen(&TetrisRP, pen);
  142.     point = &points[0];
  143.     AreaMove(&TetrisRP, point->x, point->y);
  144.     for (i = n - 2, point++; i >= 0; point++, i--)
  145.         AreaDraw(&TetrisRP, point->x, point->y);
  146.     AreaEnd(&TetrisRP);
  147.  
  148.     if (depth > 1) {
  149.         pen = 2;
  150.         SetAPen(&TetrisRP, 2);
  151.         ppt = point = &points[0];
  152.         Move(&TetrisRP, point->x, point->y);
  153.         for (i = n - 2, point++; i >= 0; point++, i--) {
  154.             switch (pen) {
  155.             case 1:
  156.                 if ((ppt->y > point->y) || (ppt->x < point->x)) {
  157.                     pen = 2;
  158.                     SetAPen(&TetrisRP, 2);
  159.                 }
  160.                 break;
  161.             case 2:
  162.                 if ((ppt->y < point->y) || (ppt->x > point->x)) {
  163.                     pen = 1;
  164.                     SetAPen(&TetrisRP, 1);
  165.                 }
  166.                 break;
  167.             }
  168.             Draw(&TetrisRP, point->x, point->y);
  169.             ppt = point;
  170.         }
  171.         SetAPen(&TetrisRP, 1);
  172.         Draw(&TetrisRP, point->x + 1, point->y);
  173.     }
  174. }
  175.  
  176. BOOL 
  177. InitTetrisImages(WORD depth, WORD xsize, WORD ysize)
  178. {
  179.     struct BitMap *bm;
  180.     int i, j;
  181.  
  182.  
  183.  
  184.     InitRastPort(&TetrisRP);
  185.     InitArea(&TetrisAI, TetrisAIBuf, 12);
  186.     TetrisRP.AreaInfo = &TetrisAI;
  187.  
  188.     if (!depth) return FALSE;
  189.  
  190.     i = xsize * 4; j = ysize * 4;
  191.     InitTmpRas(&TetrisTR, AllocRaster(i, j), RASSIZE(i, j));
  192.     TetrisRP.TmpRas = &TetrisTR;
  193.  
  194.     for (i = 0; i < NBITMAPS; i++) {
  195.         TSizes[i].x = TSSizes[i].x * xsize;
  196.         TSizes[i].y = TSSizes[i].y * ysize;
  197.         bm = &TBitMaps[i];
  198.         InitBitMap(bm, depth, TSizes[i].x, TSizes[i].y);
  199.         bm->Planes[0] = AllocMem(bm->BytesPerRow * bm->Rows * bm->Depth, MEMF_CHIP | MEMF_CLEAR);
  200.         //bm->Planes[0] = AllocRaster(TSizes[i].x, TSizes[i].y * depth);
  201.         if (!bm->Planes[0]) break;
  202.     }
  203.     if (i == NBITMAPS) {
  204.         for (i = 0; i < NBITMAPS; i++) {
  205.  
  206.             bm = &TBitMaps[i];
  207.             //BltClear(bm->Planes[0], bm->BytesPerRow * bm->Rows * bm->Depth, 0);
  208.             for (j = 1; j < depth; j++)
  209.                 bm->Planes[j] = bm->Planes[j - 1] + bm->BytesPerRow;
  210.             bm->BytesPerRow *= depth;
  211.  
  212.             DrawTetrisImage(bm, i, depth, xsize, ysize);
  213.         }
  214.         return TRUE;
  215.     }
  216.     for (i--; i >= 0; i--) {
  217.         if (TBitMaps[i].Planes[0])
  218.             //FreeRaster(TBitMaps[i].Planes[0], TSizes[i].x, TSizes[i].y * depth);
  219.             FreeMem(TBitMaps[i].Planes[0], TBitMaps[i].BytesPerRow * TBitMaps[i].Rows * TBitMaps[i].Depth);
  220.     }
  221.     return FALSE;
  222. }
  223.  
  224. void 
  225. FreeTetrisImages(WORD depth, WORD xsize, WORD ysize)
  226. {
  227.     int i;
  228.     if (TetrisTR.RasPtr) FreeRaster(TetrisTR.RasPtr, xsize * 4, ysize * 4);
  229.     for (i = 0; i < NBITMAPS; i++) {
  230.         if (TBitMaps[i].Planes[0]) {
  231.             //FreeRaster(TBitMaps[i].Planes[0], TSizes[i].x, TSizes[i].y * depth);
  232.             FreeMem(TBitMaps[i].Planes[0], TBitMaps[i].BytesPerRow * TBitMaps[i].Rows);
  233.         }
  234.     }
  235. }
  236.  
  237.